package com.yanqu.road.server.cmd.disperseGold;

import com.yanqu.road.entity.disperseGold.DisperseGoldInfo;
import com.yanqu.road.entity.disperseGold.GetDisperseGoldRewardResult;
import com.yanqu.road.entity.log.LogDisperseClientAction;
import com.yanqu.road.pb.disperseGold.DisperseGoldProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.disperseGold.DisperseGoldModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.DisperseGoldMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_DISPERSEGOLD_GET_REWARD , desc = "领取送财奖励")
public class GetRewardCmd extends AbstractCommand {
    private long userId;
    private long disperseId;

    private String reward;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DisperseGoldProto.DisperseGoldGetRewardReqMsg reqMsg = DisperseGoldProto.DisperseGoldGetRewardReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        disperseId = reqMsg.getDisperseId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DisperseGoldInfo disperseGoldInfo = DisperseGoldMgr.getDisperseGoldInfo(userId, disperseId);
        if (disperseGoldInfo == null) {
            ret = GameErrorCode.E_DISPERSEGOLD_NOT_FIND;
            return;
        }
        if (DisperseGoldMgr.isDisperseGoldEnd(disperseGoldInfo)) {
            ret = GameErrorCode.E_DISPERSEGOLD_TIME_OVER;
            AutoLogMgr.add(new LogDisperseClientAction(player.getUserId(), disperseId, 3, System.currentTimeMillis(), ""));
            return;
        }
        GetDisperseGoldRewardResult result = player.getModule(DisperseGoldModule.class).getDisperseGoldReward(disperseGoldInfo);
        if (result.getRet() != 0) {
            ret = result.getRet();
            return;
        }
        reward = result.getReward();
    }

    @Override
    public void sendData(GamePlayer player) {
        DisperseGoldProto.DisperseGoldGetRewardRespMsg.Builder respMsg = DisperseGoldProto.DisperseGoldGetRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            respMsg.setReward(reward);
        }
        player.sendPacket(Protocol.U_DISPERSEGOLD_GET_REWARD, respMsg);
    }
}
